package com.nmf.site.biz.pay.controller;

import java.io.IOException;
import java.util.SortedMap;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.IOUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.nmf.site.biz.pay.wechat.conf.HwWechatConfVo;
import com.nmf.site.biz.pay.wechat.entity.WechatPayNotify;
import com.nmf.site.biz.pay.wechat.service.NotifyService;
import com.nmf.site.biz.pay.wechat.util.TenMD5Util;
import com.nmf.site.biz.pay.wechat.util.TenpayUtil;

@RestController
@RequestMapping("pay/wechat")
public class WechatNotifyController {
	Logger log = LoggerFactory.getLogger(WechatNotifyController.class);
	@Autowired
	NotifyService wechatNotifyService;
	
	
	@RequestMapping(value = "v1.0/p/notify", method = RequestMethod.POST)
	@ResponseBody
	public String wechatNotifyPay(HttpServletRequest request) {
		try {
			String xmlParam = IOUtils.toString(request.getInputStream(), "UTF-8");
			log.info(xmlParam);
			Document document = Jsoup.parse(xmlParam, "UTF-8");
			SortedMap<String, String> smap = TenpayUtil.ele2map(document.getElementsByTag("xml").get(0));
			String singSource = smap.remove("sign");
			String signD = TenMD5Util.createSign(smap, HwWechatConfVo.getSecurityCode(), HwWechatConfVo.getCharsetStr());

			if (signD.equals(singSource)) {
				WechatPayNotify wechatPayNotify = getWechatPayNotify(xmlParam);
				String nojson = JSON.toJSONString(wechatPayNotify);
				
				if ("SUCCESS".equalsIgnoreCase(wechatPayNotify.getReturn_code())) {
					wechatNotifyService.notifyWechat(wechatPayNotify, xmlParam);
				}
				log.info("订单 "+wechatPayNotify.getOut_trade_no()+"支付成功");
				log.info("message is :"+nojson);
				return "<xml><return_code><![CDATA[SUCCESS]]></return_code>  <return_msg><![CDATA[OK]]></return_msg></xml>";
			}

		} catch (IOException e) {
			log.error("", e);
		}
		return "failed";
	}
	
	public WechatPayNotify getWechatPayNotify(String xmlStr) {
		WechatPayNotify wechatPayNotify = new WechatPayNotify();
		Document document = Jsoup.parse(xmlStr, "UTF-8");

		String appid = TenpayUtil.getDomTextByTag(document, "appid");
		String attach = TenpayUtil.getDomTextByTag(document, "attach");
		String bank_type = TenpayUtil.getDomTextByTag(document, "bank_type");
		String cash_fee = TenpayUtil.getDomTextByTag(document, "cash_fee");
		String fee_type = TenpayUtil.getDomTextByTag(document, "fee_type");
		String is_subscribe = TenpayUtil.getDomTextByTag(document, "is_subscribe");
		String mch_id = TenpayUtil.getDomTextByTag(document, "mch_id");
		String nonce_str = TenpayUtil.getDomTextByTag(document, "nonce_str");

		String openid = TenpayUtil.getDomTextByTag(document, "openid");

		String out_trade_no = TenpayUtil.getDomTextByTag(document, "out_trade_no");
		String result_code = TenpayUtil.getDomTextByTag(document, "result_code");
		String return_code = TenpayUtil.getDomTextByTag(document, "return_code");
		String sign = TenpayUtil.getDomTextByTag(document, "sign");
		String time_end = TenpayUtil.getDomTextByTag(document, "time_end");
		String total_fee = TenpayUtil.getDomTextByTag(document, "total_fee");
		String trade_type = TenpayUtil.getDomTextByTag(document, "trade_type");
		String transaction_id = TenpayUtil.getDomTextByTag(document, "transaction_id");

		wechatPayNotify.setAppid(appid);
		wechatPayNotify.setAttach(attach);
		wechatPayNotify.setBank_type(bank_type);
		// wechatPayNotify.setCoupon_fee(coupon_fee);
		// wechatPayNotify.setDevice_info(device_info);
		// wechatPayNotify.setErr_code(err_code);
		// wechatPayNotify.setErr_code_des(err_code_des);
		wechatPayNotify.setFee_type(fee_type);
		wechatPayNotify.setIs_subscribe(is_subscribe);
		wechatPayNotify.setMch_id(mch_id);
		wechatPayNotify.setNonce_str(nonce_str);
		wechatPayNotify.setOpenid(openid);
		wechatPayNotify.setOut_trade_no(out_trade_no);
		wechatPayNotify.setResult_code(result_code);
		wechatPayNotify.setReturn_code(return_code);
		// wechatPayNotify.setReturn_msg(return_msg);
		wechatPayNotify.setSign(sign);
		wechatPayNotify.setTime_end(time_end);
		wechatPayNotify.setTotal_fee(Double.valueOf(total_fee) * 0.01D + "");
		wechatPayNotify.setTrade_type(trade_type);
		wechatPayNotify.setTransaction_id(transaction_id);

		return wechatPayNotify;
	}
}
